package com.mjk.manager.magicapi.starter;

import com.mjk.manager.magicapi.core.core.config.MagicAPIProperties;
import com.mjk.manager.magicapi.core.core.service.impl.ApiInfoMagicResourceStorage;
import com.mjk.manager.magicapi.core.core.service.impl.RequestMagicDynamicRegistry;
import com.mjk.manager.magicapi.core.datasource.model.MagicDynamicDataSource;
import com.mjk.manager.magicapi.core.datasource.service.DataSourceInfoMagicResourceStorage;
import com.mjk.manager.magicapi.core.datasource.service.DataSourceMagicDynamicRegistry;
import com.mjk.manager.magicapi.core.function.service.FunctionInfoMagicResourceStorage;
import com.mjk.manager.magicapi.core.function.service.FunctionMagicDynamicRegistry;
import com.mjk.manager.magicapi.core.utils.Mapping;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.AutoConfigureAfter;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Lazy;
import org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping;

@Configuration
@AutoConfigureAfter(MagicModuleConfiguration.class)
public class MagicDynamicRegistryConfiguration {


    private final MagicAPIProperties properties;

    @Autowired
    @Lazy
    private RequestMappingHandlerMapping requestMappingHandlerMapping;


    public MagicDynamicRegistryConfiguration(MagicAPIProperties properties) {
        this.properties = properties;
    }

    @Bean
    @ConditionalOnMissingBean
    public ApiInfoMagicResourceStorage apiInfoMagicResourceStorage() {
        return new ApiInfoMagicResourceStorage(properties.getPrefix());
    }

    @Bean
    @ConditionalOnMissingBean
    public RequestMagicDynamicRegistry magicRequestMagicDynamicRegistry(ApiInfoMagicResourceStorage apiInfoMagicResourceStorage) throws NoSuchMethodException {
        return new RequestMagicDynamicRegistry(apiInfoMagicResourceStorage, Mapping.create(requestMappingHandlerMapping, properties.getWeb()), properties.isAllowOverride(), properties.getPrefix());
    }

    @Bean
    @ConditionalOnMissingBean
    public FunctionInfoMagicResourceStorage functionInfoMagicResourceStorage() {
        return new FunctionInfoMagicResourceStorage();
    }

    @Bean
    @ConditionalOnMissingBean
    public FunctionMagicDynamicRegistry functionMagicDynamicRegistry(FunctionInfoMagicResourceStorage functionInfoMagicResourceStorage) {
        return new FunctionMagicDynamicRegistry(functionInfoMagicResourceStorage);
    }

    @Bean
    @ConditionalOnMissingBean
    public DataSourceInfoMagicResourceStorage dataSourceInfoMagicResourceStorage() {
        return new DataSourceInfoMagicResourceStorage();
    }

    @Bean
    @ConditionalOnMissingBean
    public DataSourceMagicDynamicRegistry dataSourceMagicDynamicRegistry(DataSourceInfoMagicResourceStorage dataSourceInfoMagicResourceStorage, MagicDynamicDataSource magicDynamicDataSource) {
        return new DataSourceMagicDynamicRegistry(dataSourceInfoMagicResourceStorage, magicDynamicDataSource);
    }

}
